<template>
  <transition name="alert-fade">
    <div v-if="visible" class="alert-overlay">
      <div class="alert-box">
        <div class="alert-title">{{ title }}</div>
        <div class="alert-message">{{ message }}</div>
        <button @click="closeAlert">OK</button>
      </div>
    </div>
  </transition>
</template>

<script>
import { ref } from 'vue';

export default {
  name: 'CustomAlert',
  props: {
    title: {
      type: String,
      required: true
    },
    message: {
      type: String,
      required: true
    }
  },
  setup() {
    const visible = ref(false);

    const showAlert = () => {
      visible.value = true;
    };

    const closeAlert = () => {
      visible.value = false;
      // 可以触发一个自定义事件来通知父组件弹窗已关闭
      // emit('closed');
    };

    // 在组件挂载时显示弹窗（或者根据实际需要来决定何时显示）
    // 这里为了演示，我们不在 mounted 钩子中自动调用 showAlert
    // 你可以在父组件中通过某种方式（如点击事件）来控制这个弹窗的显示

    return {
      visible,
      closeAlert,
      showAlert
    };
  }
};
</script>

<style scoped>
.alert-fade-enter-active,
.alert-fade-leave-active {
  transition: opacity 0.3s;
}

.alert-fade-enter,
.alert-fade-leave-to {
  opacity: 0;
}

.alert-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.alert-box {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.alert-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.alert-message {
  margin-bottom: 20px;
}

button {
  padding: 10px 20px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}
</style>